//
//  MenuViewController.m
//  Menu
//
//  Created by ZHY on 2017/7/26.
//  Copyright © 2017年 ZHY. All rights reserved.
//

#define cellHeight 44

#import "MenuViewController.h"


static NSString *cellID = @"MenuViewControllerCell";

@interface MenuViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) CGRect tableViewRect;

@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark - setupUI 
-(void)setupUI {

    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
}

#pragma mark - selector
/**
 *  显示菜单栏
 *
 *  @param frame 在baseVc的范围
 *  @param titles 需要显示的标题上
 *  @param baseVc 菜单栏显示在某个控制器上
 */
-(void)showMenuWithFrame:(CGRect)frame titles:(NSArray<NSString *> *)titles inBaseVc:(UIViewController *)baseVc {
    self.tableViewRect = frame;
    if (titles) {
        self.titles = titles;
    }
    self.view.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0];
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, 0);
    [self.tableView reloadData];
    [baseVc addChildViewController:self];
    [baseVc.view addSubview:self.view];
   
    //下拉动画
    [UIView animateWithDuration:0.38 animations:^{
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.titles.count * cellHeight);
        self.view.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
    }];
    
}


/**
 *  隐藏菜单栏
 */
-(void)hideMenu {
    
    [UIView animateWithDuration:0.38 animations:^{
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, 0);
        self.view.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0];
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

/**
 *  点击了空白处
 */
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self hideMenu];
}

#pragma mark - table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.titles[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.backgroundColor = [UIColor colorWithRed:65/255.0 green:66/255.0 blue:73/255.0 alpha:1];
    
    return cell;
}

#pragma mark - table view delegate 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectMenuWithIndex:title:)]) {
        [self.delegate selectMenuWithIndex:indexPath.row title:self.titles[indexPath.row]];
        [self hideMenu];
    }
}

#pragma mark - set or get 
-(UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.tableViewRect.origin.x, self.tableViewRect.origin.y, self.tableViewRect.size.width, 0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

@end
